
package jpoco.android;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

import java.util.*;

import jpoco.client.*;

public class ContactsActivity extends AbstractActivity 
{
	private Button btnGetContacts;
	private Button btnImport;
    private List<Contact> contacts;

    public static final String ACTION_VIEW = "jpoco.android.action.VIEW";
    
	static
	{
		System.getProperties().put("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
		System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "trace");
	}
	
    public ContactsActivity()
    {
    	super();
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        btnImport = new Button(this);
        btnImport.setText("Import");
        btnImport.setOnClickListener(new OnClickListener()
        {

			public void onClick(View v)
			{
				btnImport.setEnabled(false);

				saveContactsAsync(contacts);
				
			}
        	
        });
        
         
        btnGetContacts = new Button(this);
        btnGetContacts.setText("Download contacts from " + AndroidApplication.getCurrentProvider());
        btnGetContacts.setVisibility(View.VISIBLE);
        btnGetContacts.setOnClickListener(new OnClickListener() 
        {

			public void onClick(View v)
			{
				
				btnGetContacts.setText("please wait...");
				btnGetContacts.setEnabled(false);
				
				getContactsAsync(AndroidApplication.getCurrentClient());
		
			}
        	
        });
        

        
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
       
        layout.addView(btnGetContacts);
        
        setContentView(layout);
        
    }

    protected void saveContactsAsync(List<Contact> contactList)
    {
		final ContactManager cm = new ContactManager(ContactsActivity.this.getContentResolver());
		
		startThread("save contacts thread", new Runnable()
							{
								public void run()
								{
									final StringBuffer msg = new StringBuffer();
									
									try
									{
										cm.save(contacts);
										msg.append(AndroidApplication.getCurrentProvider() + " contacts saved");
									}
									catch (Exception ex)
									{
										msg.append("Fail! " + ex.getClass());
									}
									
										
									ContactsActivity.this.runOnUiThread(new Runnable()
										{
											public void run()
											{
												MessageBox.show(ContactsActivity.this, msg);
											}
										});
								}
								
								
							});
		
		
    }
    
    protected void getContactsAsync(final Client c)
    {
		Runnable getContacts = new Runnable()
		{
			public void run()
			{
				try
				{
					if (c.getClientSettings().isOpenSocial())
					{
						contacts = c.getFriends();
					}
					else
					{
						contacts = c.getContacts();
					}
					System.out.println("Got " + contacts.size() + " contacts from " + c.getClientSettings().getProviderName());
				}
				catch (Exception ex)
				{
					ContactsActivity.this.onFail(ex);
					contacts = new ArrayList<Contact>();
				}
				
				Runnable updateUI = new Runnable()
				{
					public void run()
					{
						
				        btnGetContacts.setVisibility(View.GONE);
				        if (contacts.size() > 0)
				        {
					        ContactListView contactsView = new ContactListView(ContactsActivity.this);
				        	contactsView.addHeaderView(btnImport);
				        	contactsView.setContacts(contacts);
					        ContactsActivity.this.setContentView(contactsView);
				        }
				        else
				        {
				        	TextView msg = new TextView(ContactsActivity.this);
				        	msg.setText("No contacts found.");
					        ContactsActivity.this.setContentView(msg);
				        }
					} // run()
				};
				
				runOnUiThread(updateUI);
		        
			} // run()
			
		};
		
		startThread(getContacts);
        
    	
    }
}


